﻿package {
	import Nonoba.api.*;
	
	import com.common.screens.PlayScreen;
	import com.shotinthedark.MultiplayerGamePlay;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	public class ShotInTheDark extends MovieClip{
		private var bmd:BitmapData
		private var bm:Bitmap
		private var tempCanvas:Sprite
		private var tempGrapichs:Graphics
		private var isInDraw:Boolean = false
		private var connection:Connection
		private var isInited:Boolean = false
		private var playScreen : PlayScreen;
		private var gamePlay : MultiplayerGamePlay;
		private var lines:Object = {}
		function ShotInTheDark(){
			//Halt the flash file from going to the next frame
			stop();
			
			//Attach events
			
			//Init MP API
			connection = NonobaAPI.MakeMultiplayer(stage);
			playScreen = new PlayScreen();
			addChild(playScreen);
			connection.addEventListener(ConnectionEvent.INIT, function(e){
				isInited = true
				gamePlay = new MultiplayerGamePlay(playScreen, connection);
			})
			
			//Handle error state
			connection.addEventListener(ConnectionEvent.DISCONNECT, function(e){
				gotoAndStop("error")
			})

			
		}

//		private function handleMouseDown(e:Event){
//			if(isInited){
//				isInDraw = true
//				connection.Send("start", mouseX, mouseY)
//			}
//		}
//		
//		private function handleMouseUp(e:Event){
//			if(isInited){
//				isInDraw = false
//				connection.Send("stop")
//			}
//		}
//		
//		private function handleMouseMove(e:Event){
//			if(isInDraw){
//				connection.Send("move", mouseX, mouseY)
//			}
//		}
//
//		private function onMessage(e:Object){
//			var message:Object = e.message;
//			switch(message.Type){
//				case "start":{
//					
//					//Show who is drawing
//					var nameLabel:TextField = new TextField()
//					nameLabel.text = message.GetString(1);
//					nameLabel.selectable = false
//					nameLabel.x = message.GetInt(2) - nameLabel.textWidth/2
//					nameLabel.y = message.GetInt(3) - nameLabel.textHeight/2
//					addChild(nameLabel)
//					
//					//Destory the name after 500ms
//					setTimeout(function(){
//						removeChild(nameLabel) 
//					},500)
//					
//					
//					lines[message.GetString(0)] = new Point(message.GetInt(2), message.GetInt(3))
//					break
//				}
//				case "stop":{
//					delete lines[message.GetString(0)]
//					break;
//				}
//				case "move":{
//					var id:String = message.GetString(0)
//					if(lines[id]){
//						var newPoint:Point = new Point(message.GetInt(1), message.GetInt(2))
//						drawLine(lines[id], newPoint)
//						lines[id] = newPoint
//					}
//					break;
//				}
//				default:{
//					trace("Got unhandled message > " + message)
//				}
//			}
//		}
		
	}
}
